﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Hotel.Server.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class CommonsController : ControllerBase
    {


        /// <summary>
        /// 图片上传
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UpoadImgs(IFormFile file)
        {

            try
            {
                //1  上传的是不是图片
                var path = file.ContentType;
                //定义图片
                var foot = new List<string>
                {

                };
               

                //2  图片大小不能超过2m
                var m = 1024 * 1024 * 2;
                if (file.Length > m) 
                {
                    return Ok(new {
                        msg = "上传的图片不能超过2M"
                    });
                
                }
                //3完成图片上传
                var str = Guid.NewGuid();
                var p = Path.Combine("imgs", str + file.FileName);
                using (var stream= new FileStream(p, FileMode.Create )) 
                {
                 file.CopyTo(stream);
                
                }
                return Ok(new
                {
                    code = 1,
                    data= "/imgs/"+str+file.FileName,

                });








            }
            catch (Exception)
            {

                throw;
            }


        }



    }
}
